package com.achao.basecommon.config;

import feign.Logger;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.stream.Collectors;

/**
 * 描述：fei配置类
 *
 * @Author：yangchao
 * @CreateDate：2023/2/9 11:48
 */
@Configuration
public class ConfigBean {
    /**
     * OpenFeign 日志增强
     * 配置 OpenFeign 记录哪些内容
     */
    @Bean
    public Logger.Level feginLoggerLevel() {
        return Logger.Level.FULL;
    }

    //在spring-boot2.1.x版本是不用手动注入HttpMessageConverters的，因为可以自动配置的, 见HttpMessageConvertersAutoConfiguration。
    //但是在spring-boot2.2.x版本HttpMessageConvertersAutoConfiguration有所改动，加了个@Conditional(NotReactiveWebApplicationCondition.class) ，
    // 因为gateway是ReactiveWeb，所以针对HttpMessageConverters的自动配置就不生效了，故需要手动注入HttpMessageConverters
    /** gateway缺少手动转换器，所以手动注入bean就解决了这个feign无法调用得问题 */
    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }

}